
import math

class vector2D:
    def __init__(self,_x,_y):
        self.x = _x
        self.y = _y        
    def __add__(self,vec):
        return vector2D(self.x+vec.x,self.y+vec.y)
    def __sub__(self,vec):
        return vector2D(self.x-vec.x,self.y-vec.y)
    def __mul__(self,mul):
        return vector2D(self.x*mul,self.y*mul)    
    def __rmul__(self,mul):
        return vector2D(self.x*mul,self.y*mul) 
    def __div__(self,div):                
        return vector2D(self.x/div,self.y/div)        
    def __neg__(self):
        return vector2D(-self.x,-self.y)
    def length(self):
        return math.sqrt(self.x*self.x+self.y*self.y)
    def __ne__(self,other):
        return self.x!=other.x or self.y!=other.y           
    def normalize(self):        
        length = self.length()
        if(length>0):
            self.x/= length
            self.y/= length
    
    def truncate(self,max):    
        if(self.length()>max):
            self.normalize()
            self*=max
    def lengthsq(self):
        return self.x*self.x+self.y*self.y
    def perp(self):
        return vector2D(-self.y,self.x)
    def dot(self,vec):
        return self.x*vec.x+self.y*vec.y
    def sign( self, v2 ):
        if self.y * v2.x > self.x * v2.y:
            return False
        else:
            return True
            
    def angle( self, _rhs ):
        _Value = self.dot( _rhs ) / ( self.length() * _rhs.length() )
        _Value = min( max( _Value, -1.0 ), 1.0 )
        _Angle = math.acos( _Value )
        if not self.sign( _rhs ):
            _Angle = -_Angle
            
        return _Angle
        
        
UNIT_X = vector2D( 1, 0 )
UNIT_Y = vector2D( 0, 1 )

NEGATIVE_UNIT_X = vector2D( -1, 0 )
NEGATIVE_UNIT_Y = vector2D( 0, -1 )
        
def normalize(v):
    nvec = vector2D(v.x,v.y)
    length = nvec.length()
    if(length>0):
       nvec.x/= length
       nvec.y/= length
    return nvec

def rotate( _vector, _angle ):
    _cos = math.cos( _angle )
    _sin = math.sin( _angle )
    _rx = (_cos * _vector.x) + (-_sin * _vector.y)
    _ry = (_sin * _vector.x) + ( _cos * _vector.y)
    
    return vector2D(_rx, _ry)
    
def vec2DDistance(v1,v2):
    distvec= v2-v1
    return distvec.length()